#include <QtTest/QtTest>
#include <QJsonObject>
#include "financial_income_analytics.h"

class TestIncomeAnalytics : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void cleanupTestCase();
    void testCalculateChangeRate();
    void testValidIncomeData();
    void testValidateAndCalculateMetrics();
    void testEdgeCases();

private:
    financial_income_analytics *analytics;
};

void TestIncomeAnalytics::initTestCase()
{
    analytics = new financial_income_analytics();
}

void TestIncomeAnalytics::cleanupTestCase()
{
    delete analytics;
}

void TestIncomeAnalytics::testCalculateChangeRate()
{
    // 正常情况测试
    QCOMPARE(analytics->calculateChangeRate(100.0, 80.0), 25.0);  // (100-80)/80*100 = 25%
    QCOMPARE(analytics->calculateChangeRate(80.0, 100.0), -20.0); // (80-100)/100*100 = -20%
    
    // 零值测试
    QCOMPARE(analytics->calculateChangeRate(0.0, 0.0), 0.0);
    QCOMPARE(analytics->calculateChangeRate(100.0, 0.0), 999.9);
    QCOMPARE(analytics->calculateChangeRate(0.0, 100.0), -100.0);
    
    // 负数测试（应该返回0，因为收入不能为负）
    QCOMPARE(analytics->calculateChangeRate(-100.0, 80.0), 0.0);
    QCOMPARE(analytics->calculateChangeRate(100.0, -80.0), 0.0);
    
    // 极值测试
    QCOMPARE(analytics->calculateChangeRate(10000000.0, 1000000.0), 900.0); // 应该被限制在999.9
    QCOMPARE(analytics->calculateChangeRate(100.0, 0.001), 999.9); // 接近除零的情况
}

void TestIncomeAnalytics::testValidIncomeData()
{
    // 有效数据测试
    QVERIFY(analytics->isValidIncomeData(100.0));
    QVERIFY(analytics->isValidIncomeData(0.0));
    QVERIFY(analytics->isValidIncomeData(9999999.99));
    
    // 无效数据测试
    QVERIFY(!analytics->isValidIncomeData(-100.0));  // 负数
    QVERIFY(!analytics->isValidIncomeData(10000001.0)); // 超出范围
    
    // 特殊值测试
    QVERIFY(!analytics->isValidIncomeData(qInf()));   // 无穷大
    QVERIFY(!analytics->isValidIncomeData(qNaN()));    // 非数字
}

void TestIncomeAnalytics::testValidateAndCalculateMetrics()
{
    // 创建测试数据
    QJsonObject testData;
    testData["todayIncome"] = "120000.00";
    testData["yesterdayIncome"] = "100000.00";
    testData["thisMonthIncome"] = "3000000.00";
    testData["lastMonthIncome"] = "2500000.00";
    testData["thisYearIncome"] = "40000000.00";
    testData["lastYearIncome"] = "35000000.00";
    testData["thisHourIncome"] = "5000.00";
    testData["lastHourIncome"] = "4000.00";
    
    double dailyChange, monthlyChange, yearlyChange, realtimeChange;
    
    // 测试正常数据
    analytics->validateAndCalculateMetrics(testData, dailyChange, monthlyChange, yearlyChange, realtimeChange);
    
    QCOMPARE(dailyChange, 20.0);     // (120000-100000)/100000*100 = 20%
    QCOMPARE(monthlyChange, 20.0);   // (3000000-2500000)/2500000*100 = 20%
    QCOMPARE(yearlyChange, 14.285714285714286); // (40000000-35000000)/35000000*100 ≈ 14.29%
    QCOMPARE(realtimeChange, 25.0);  // (5000-4000)/4000*100 = 25%
    
    // 测试无效数据
    QJsonObject invalidData;
    invalidData["todayIncome"] = "-100.00";  // 无效的负数
    invalidData["yesterdayIncome"] = "100000.00";
    invalidData["thisMonthIncome"] = "3000000.00";
    invalidData["lastMonthIncome"] = "2500000.00";
    invalidData["thisYearIncome"] = "40000000.00";
    invalidData["lastYearIncome"] = "35000000.00";
    invalidData["thisHourIncome"] = "5000.00";
    invalidData["lastHourIncome"] = "4000.00";
    
    analytics->validateAndCalculateMetrics(invalidData, dailyChange, monthlyChange, yearlyChange, realtimeChange);
    
    QCOMPARE(dailyChange, 0.0);      // 无效数据应该返回0
    QCOMPARE(monthlyChange, 20.0);   // 有效数据正常计算
    QCOMPARE(yearlyChange, 14.285714285714286);
    QCOMPARE(realtimeChange, 25.0);
}

void TestIncomeAnalytics::testEdgeCases()
{
    // 测试边界情况
    QCOMPARE(analytics->calculateChangeRate(0.01, 0.01), 0.0);
    QCOMPARE(analytics->calculateChangeRate(9999999.99, 1.0), 999.9);
    QCOMPARE(analytics->calculateChangeRate(1.0, 9999999.99), -100.0);
    
    // 测试精度
    double result = analytics->calculateChangeRate(100.001, 100.0);
    QVERIFY(qAbs(result - 0.001) < 0.0001);
}

QTEST_MAIN(TestIncomeAnalytics)
#include "test_income_analytics.moc"